package com.ebay.buyer;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.ebay.model.*;
import com.ebay.services.Bidding;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PlaceBid extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public boolean isPlaceBid() {
		return placeBid;
	}

	public void setPlaceBid(boolean placeBid) {
		this.placeBid = placeBid;
	}

	public float getBidAmount() {
		return bidAmount;
	}

	public void setBidAmount(float bidAmount) {
		this.bidAmount = bidAmount;
	}

	public float getCurrentBid() {
		return currentBid;
	}

	public void setCurrentBid(float currentBid) {
		this.currentBid = currentBid;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}

	public int getProduct_id() {
		return product_id;
	}

	public void setProduct_id(int product_id) {
		this.product_id = product_id;
	}
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getUser_id() {
		return user_id;
	}

	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int user_id;
	private String url = null;
	private int product_id;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Book book = null;
	private float currentBid;
	private Date endTime;
	private float bidAmount;
	private boolean placeBid = false;
	private String errorMsg;

	public PlaceBid() {
		super();
	}

	@Override
	public void setServletResponse(HttpServletResponse res) {
		this.response = res;
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		this.request = req;

	}

	public String execute()
	{
		Map<String,Object> session = ActionContext.getContext().getSession();

		//check if the user is logged in else, store the incoming url and redirect to sign in.
		if(session == null || session.get("loggedin") == null || session.get("user") == null || !((String) session.get("loggedin")).equals("true") ){
			url = "/"+ActionContext.getContext().getName()+"?"+request.getQueryString();
			System.out.println("URL : "+url); 
			return LOGIN;
		}
		System.out.println("Bidding : "+product_id);

		book = Book.getBookDetails(product_id);
		Bidding bid = new Bidding(book.getCategory_id(),book.getSubcategory_id(),book.getProduct_id());
		bid.getBiddingDetails();
		System.out.println(bid.toString());
		currentBid = bid.getBidPrice();
		endTime = bid.getEndTime();
		if(bidAmount > currentBid){
			//TODO:placebid
			Calendar now = Calendar.getInstance();
			Calendar endTime = Calendar.getInstance();
			endTime.setTime(getEndTime());
			if(endTime.after(now)){
				placeBid = true;
			}else{
				setErrorMsg("Sorry! The item you were trying to bid Expired!");
				return ERROR;
			}
		}
		return SUCCESS;

	}
}
